home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / packages / makeinfo.el.z / makeinfo.el
Encoding:
Text File  |  1998-05-21  |  8.9 KB  |  259 lines

  1. ;;; makeinfo.el --- run makeinfo conveniently
  2.  
  3. ;; Copyright (C) 1991, 1993 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Robert J. Chassell      
  6. ;; Maintainer: FSF
  7.  
  8. ;; This file is part of XEmacs.
  9.  
  10. ;; XEmacs is free software; you can redistribute it and/or modify it
  11. ;; under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; XEmacs is distributed in the hope that it will be useful, but
  16. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18. ;; General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  22. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. ;; Boston, MA 02111-1307, USA.
  24.  
  25. ;;; Synched up with: FSF 19.34.
  26.  
  27. ;;; Commentary:
  28.  
  29. ;;; The Texinfo mode `makeinfo' related commands are:
  30.  
  31. ;; makeinfo-region      to run makeinfo on the current region.
  32. ;; makeinfo-buffer      to run makeinfo on the current buffer, or
  33. ;;                        with optional prefix arg, on current region
  34. ;; kill-compilation     to kill currently running makeinfo job
  35. ;; makeinfo-recenter-makeinfo-buffer  to redisplay *compilation* buffer
  36.  
  37. ;;; Keybindings (defined in `texinfo.el')
  38.  
  39. ;; makeinfo bindings
  40. ; (define-key texinfo-mode-map "\C-c\C-m\C-r" 'makeinfo-region)
  41. ; (define-key texinfo-mode-map "\C-c\C-m\C-b" 'makeinfo-buffer)
  42. ; (define-key texinfo-mode-map "\C-c\C-m\C-k" 'kill-compilation)
  43. ; (define-key texinfo-mode-map "\C-c\C-m\C-l"
  44. ;   'makeinfo-recenter-compilation-buffer)
  45.  
  46. ;;; Code:
  47.  
  48. ;;; Variables used by `makeinfo'
  49.  
  50. (require 'compile)
  51.  
  52. (defgroup makeinfo nil
  53.   "Run makeinfo conveniently."
  54.   :group 'docs)
  55.  
  56.  
  57. (defcustom makeinfo-run-command "makeinfo"
  58.   "*Command used to run `makeinfo' subjob.
  59. The name of the file is appended to this string, separated by a space."
  60.   :type 'string
  61.   :group 'makeinfo)
  62.  
  63. (defcustom makeinfo-options "--fill-column=70"
  64.   "*String containing options for running `makeinfo'.  
  65. Do not include `--footnote-style' or `--paragraph-indent';
  66. the proper way to specify those is with the Texinfo commands
  67. `@footnotestyle` and `@paragraphindent'."
  68.   :type 'string
  69.   :group 'makeinfo)
  70.  
  71. (require 'texinfo)
  72.  
  73. (defvar makeinfo-compilation-process nil
  74.   "Process that runs `makeinfo'.  Should start out nil.")
  75.  
  76. (defvar makeinfo-temp-file nil
  77.   "Temporary file name used for text being sent as input to `makeinfo'.")
  78.  
  79. (defvar makeinfo-output-file-name nil
  80.   "Info file name used for text output by `makeinfo'.")
  81.  
  82.  
  83. ;;; The `makeinfo' function definitions
  84.  
  85. (defun makeinfo-region (region-beginning region-end)
  86.   "Make Info file from region of current Texinfo file, and switch to it.
  87.  
  88. This command does not offer the `next-error' feature since it would
  89. apply to a temporary file, not the original; use the `makeinfo-buffer'
  90. command to gain use of `next-error'."
  91.   
  92.   (interactive "r")
  93.   (let (filename-or-header
  94.         filename-or-header-beginning
  95.         filename-or-header-end)
  96.     ;; Cannot use `let' for makeinfo-temp-file or
  97.     ;; makeinfo-output-file-name since `makeinfo-compilation-sentinel'
  98.     ;; needs them.
  99.  
  100.     (setq makeinfo-temp-file
  101.           (concat
  102.            (make-temp-name
  103.             (substring (buffer-file-name)
  104.                        0 
  105.                        (or (string-match "\\.tex" (buffer-file-name)) 
  106.                            (length (buffer-file-name)))))
  107.            ".texinfo"))
  108.     
  109.     (save-excursion
  110.       (save-restriction
  111.         (widen)
  112.         (goto-char (point-min))
  113.         (let ((search-end (save-excursion (forward-line 100) (point))))
  114.           ;; Find and record the Info filename,
  115.           ;; or else explain that a filename is needed.
  116.           (if (re-search-forward 
  117.                "^@setfilename[ \t]+\\([^ \t\n]+\\)[ \t]*"
  118.                search-end t)
  119.               (setq makeinfo-output-file-name 
  120.                     (buffer-substring (match-beginning 1) (match-end 1)))
  121.             (error
  122.              "The texinfo file needs a line saying: @setfilename <name>"))
  123.  
  124.           ;; Find header and specify its beginning and end.
  125.           (goto-char (point-min))
  126.           (if (and 
  127.                (prog1 
  128.                    (search-forward tex-start-of-header search-end t)
  129.                  (beginning-of-line)
  130.                  ;; Mark beginning of header.
  131.                  (setq filename-or-header-beginning (point)))
  132.                (prog1 
  133.                    (search-forward tex-end-of-header nil t)
  134.                  (beginning-of-line)
  135.                  ;; Mark end of header
  136.                  (setq filename-or-header-end (point))))
  137.               
  138.               ;; Insert the header into the temporary file.
  139.               (write-region
  140.                (min filename-or-header-beginning region-beginning)
  141.                filename-or-header-end
  142.                makeinfo-temp-file nil nil)
  143.             
  144.             ;; Else no header; insert @filename line into temporary file.
  145.             (goto-char (point-min))
  146.             (search-forward "@setfilename" search-end t)
  147.             (beginning-of-line)
  148.             (setq filename-or-header-beginning (point))
  149.             (forward-line 1)
  150.             (setq filename-or-header-end (point))
  151.             (write-region
  152.              (min filename-or-header-beginning region-beginning)
  153.              filename-or-header-end
  154.              makeinfo-temp-file nil nil))
  155.           
  156.           ;; Insert the region into the file.
  157.           (write-region
  158.            (max region-beginning filename-or-header-end)
  159.            region-end
  160.            makeinfo-temp-file t nil)
  161.  
  162.           ;; Run the `makeinfo-compile' command in the *compilation* buffer
  163.           (save-excursion
  164.             (makeinfo-compile
  165.              (concat makeinfo-run-command
  166.                      " "
  167.                      makeinfo-options
  168.                      " " 
  169.                      makeinfo-temp-file)
  170.              "Use `makeinfo-buffer' to gain use of the `next-error' command"
  171.          nil)))))))
  172.  
  173. ;;; Actually run makeinfo.  COMMAND is the command to run.
  174. ;;; ERROR-MESSAGE is what to say when next-error can't find another error.
  175. ;;; If PARSE-ERRORS is non-nil, do try to parse error messages.
  176. (defun makeinfo-compile (command error-message parse-errors)
  177.   (let ((buffer
  178.      (compile-internal command error-message nil
  179.                (and (not parse-errors)
  180.                 ;; If we do want to parse errors, pass nil.
  181.                 ;; Otherwise, use this function, which won't
  182.                 ;; ever find any errors.
  183.                 '(lambda (&rest ignore)
  184.                    (setq compilation-error-list nil))))))
  185.     (set-process-sentinel (get-buffer-process buffer)
  186.               'makeinfo-compilation-sentinel)))
  187.  
  188. ;; Delete makeinfo-temp-file after processing is finished,
  189. ;; and visit Info file.
  190. ;; This function is called when the compilation process changes state.
  191. ;; Based on `compilation-sentinel' in compile.el
  192. (defun makeinfo-compilation-sentinel (proc msg)
  193.   (compilation-sentinel proc msg)
  194.   (if (and makeinfo-temp-file (file-exists-p makeinfo-temp-file))
  195.       (delete-file makeinfo-temp-file))
  196.   ;; Always use the version on disk.
  197.   (if (get-file-buffer makeinfo-output-file-name)
  198.       (progn (set-buffer makeinfo-output-file-name)
  199.          (revert-buffer t t))
  200.     (find-file makeinfo-output-file-name))
  201.   (goto-char (point-min)))
  202.  
  203. (defun makeinfo-buffer ()
  204.   "Make Info file from current buffer.
  205.  
  206. Use the \\[next-error] command to move to the next error 
  207. \(if there are errors\)."
  208.  
  209.   (interactive)
  210.   (cond ((null buffer-file-name)
  211.          (error "Buffer not visiting any file"))
  212.         ((buffer-modified-p)
  213.          (if (y-or-n-p "Buffer modified; do you want to save it? ")
  214.              (save-buffer))))
  215.   
  216.   ;; Find and record the Info filename,
  217.   ;; or else explain that a filename is needed.
  218.   (save-excursion
  219.     (goto-char (point-min))
  220.     (let ((search-end (save-excursion (forward-line 100) (point))))
  221.       (if (re-search-forward 
  222.            "^@setfilename[ \t]+\\([^ \t\n]+\\)[ \t]*"
  223.            search-end t)
  224.           (setq makeinfo-output-file-name 
  225.                 (buffer-substring (match-beginning 1) (match-end 1)))
  226.         (error
  227.          "The texinfo file needs a line saying: @setfilename <name>"))))
  228.   
  229.   (save-excursion
  230.     (makeinfo-compile
  231.      (concat makeinfo-run-command " " makeinfo-options
  232.              " " buffer-file-name)
  233.      "No more errors."
  234.      t)))
  235.  
  236. (defun makeinfo-recenter-compilation-buffer (linenum)
  237.   "Redisplay `*compilation*' buffer so most recent output can be seen.
  238. The last line of the buffer is displayed on
  239. line LINE of the window, or centered if LINE is nil."
  240.   (interactive "P")
  241.   (let ((makeinfo-buffer (get-buffer "*compilation*"))
  242.     (old-buffer (current-buffer)))
  243.     (if (null makeinfo-buffer)
  244.     (message "No *compilation* buffer")
  245.       (pop-to-buffer makeinfo-buffer)
  246.       (bury-buffer makeinfo-buffer)
  247.       (goto-char (point-max))
  248.       (recenter (if linenum
  249.             (prefix-numeric-value linenum)
  250.           (/ (window-height) 2)))
  251.       (pop-to-buffer old-buffer)
  252.       )))
  253.  
  254. ;;; Place `provide' at end of file.
  255. (provide 'makeinfo)
  256.  
  257. ;;; makeinfo.el ends here
  258.  
  259.